﻿using System;

namespace StateMachine.TextParsing
{
    public abstract class TextParser<T> : FiniteStateMachine<T>
        where T : struct
    {
        private readonly TextParserContext<T> textParserContext;

        public TextParser(TextParserContext<T> context)
            : base(context)
        {
            this.textParserContext = context;
        }

        protected TextParserContext<T> TextParserContext
        {
            get
            {
                return this.textParserContext;
            }
        }

        public void Parse(string text)
        {
            this.TextParserContext.Initialize(text);

            foreach (char character in text)
            {
                base.HandleInput(character);
                this.TextParserContext.CurrentIndex++;
            }

            this.TextParserContext.CurrentIndex = -1;
        }
    }
}
